/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>
#include "sle_connect_impl.h"
#include "cmd_input.h"
#include <vector>
#include <memory>
#include <cstring>
#include <iterator>
#include "sle_connect_interface.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
namespace {
using namespace testing::ext;
SleConnectManager *g_connectInstance;
} // namespace

class ConnectHandlerTest : public testing::Test {
public:
    static void SetUpTestCase();
    static void TearDownTestCase();
    void SetUp();
    void TearDown();  
};

void ConnectHandlerTest::SetUpTestCase() {}

void ConnectHandlerTest::TearDownTestCase() {}

void ConnectHandlerTest::SetUp() {
    if (g_connectInstance == nullptr) {
        g_connectInstance = new SleConnectManager();
    }
}

void ConnectHandlerTest::TearDown() {

}

HWTEST_F(ConnectHandlerTest, ConnectTest001, TestSize.Level1)
{
    // 准备测试数据
    SleDeviceAddress remoteAddr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
    };
    char addr[18] = "00:11:aa:bb:f1:6b"; // 直接给定合法地址

    if (strlen(addr) != 17) {
        FAIL() << "[SleConnectTestTool] Invalid address length";
        return;
    }

    // 将字符串地址转换为 SleDeviceAddress 格式
    Str2Mac(remoteAddr.addr, addr);

    SleErrorCode ret = g_connectInstance->Connect(remoteAddr);

    EXPECT_EQ(ret, SLE_ERR_CHECKOUT_FAILED);
}

HWTEST_F(ConnectHandlerTest, DisconnectTest001, TestSize.Level1) {
    // 准备测试数据
    SleDeviceAddress remoteAddr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
    };
    char addr[18] = "00:11:aa:bb:f1:6b"; // 直接给定合法地址

    if (strlen(addr) != 17) {
        FAIL() << "[SleDisconnectTestTool] Invalid address length";
        return;
    }

    // 将字符串地址转换为 SleDeviceAddress 格式
    Str2Mac(remoteAddr.addr, addr);

    // 调用实际的断开连接函数
    SleErrorCode ret = g_connectInstance->Disconnect(remoteAddr);

    // 验证返回值
    EXPECT_EQ(ret, SLE_ERR_CHECKOUT_FAILED); 
}

HWTEST_F(ConnectHandlerTest, GetConnectIdTest001, TestSize.Level1) {
    // 准备测试数据
    SleDeviceAddress remoteAddr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
    };
    uint16_t connectId;

    char addr[18] = "00:11:aa:bb:f1:6b"; // 直接给定合法地址

    if (strlen(addr) != 17) {
        FAIL() << "[SleGetConnectIdTestTool] Invalid address length";
        return;
    }

    // 将字符串地址转换为 SleDeviceAddress 格式
    Str2Mac(remoteAddr.addr, addr);

    // 调用获取连接 ID 函数
    SleErrorCode ret = g_connectInstance->GetConnectId(remoteAddr, connectId);

    // 验证返回值
    EXPECT_EQ(ret, SLE_ERR_CHECKOUT_FAILED); // 期望成功获取连接 ID
}

HWTEST_F(ConnectHandlerTest, GetConnectStateTest001, TestSize.Level1) {
    // 准备测试数据
    SleDeviceAddress remoteAddr = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
    };
    SleConnectState state;

    char addr[18] = "00:11:aa:bb:f1:6b"; // 直接给定合法地址

    if (strlen(addr) != 17) {
        FAIL() << "[SleGetConnectStateTestTool] Invalid address length";
        return;
    }

    // 将字符串地址转换为 SleDeviceAddress 格式
    Str2Mac(remoteAddr.addr, addr);
    // 调用获取连接状态函数
    SleErrorCode ret = g_connectInstance->GetConnectState(remoteAddr, state);
    // 验证返回值
    EXPECT_EQ(ret, SLE_ERR_CHECKOUT_FAILED); // 期望成功获取连接状态
}

HWTEST_F(ConnectHandlerTest, GetConnectDeviceListTest, TestSize.Level1)
{
    std::vector<SleConnectDev> devices;
    uint16_t number = 0;
    
    // 模拟 pimpl_ 和 proxy_ 的行为
    SleErrorCode ret = g_connectInstance->GetConnectDeviceList(devices, number);
    
    // 验证返回值和设备列表是否符合预期
    EXPECT_EQ(ret, SLE_ERR_CHECKOUT_FAILED);
    EXPECT_GE(devices.size(), 0);  // 确保设备列表不是负数
}

HWTEST_F(ConnectHandlerTest, UpdateConnectParamTest, TestSize.Level1)
{
    SleConnectParam param;
    uint16_t connectId = 1;
    
    SleErrorCode ret = g_connectInstance->UpdateConnectParam(connectId, param);
    
    // 验证返回值是否符合预期
    EXPECT_EQ(ret, SLE_ERR_CHECKOUT_FAILED);
}

HWTEST_F(ConnectHandlerTest, SendPairRequestTest, TestSize.Level1)
{
    SleDeviceAddress address = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
    };
    char addr[18] = "00:11:aa:bb:f1:6b";
    
    if (strlen(addr) != 17) {
        FAIL() << "[SleConnectTestTool] Invalid address length";
        return;
    }

    Str2Mac(address.addr, addr);
    
    SleErrorCode ret = g_connectInstance->SendPairRequest(address);
    
    // 验证返回值是否符合预期
    EXPECT_EQ(ret, SLE_ERR_CHECKOUT_FAILED);
}

HWTEST_F(ConnectHandlerTest, RemovePairedDeviceTest, TestSize.Level1)
{
    SleDeviceAddress address = {
        .addrType = SLE_PUBLIC_ADDRESS_TYPE,
    };
    char addr[18] = "00:11:aa:bb:f1:6b";
    
    if (strlen(addr) != 17) {
        FAIL() << "[SleConnectTestTool] Invalid address length";
        return;
    }

    Str2Mac(address.addr, addr);
    
    SleErrorCode ret = g_connectInstance->RemovePairedDevice(address);
    
    // 验证返回值是否符合预期
    EXPECT_EQ(ret, SLE_ERR_CHECKOUT_FAILED);
}

HWTEST_F(ConnectHandlerTest, RemoveAllPairedDevicesTest, TestSize.Level1)
{
    SleErrorCode ret = g_connectInstance->RemoveAllPairedDevices();
    
    // 验证返回值是否符合预期
    EXPECT_EQ(ret, SLE_ERR_CHECKOUT_FAILED);
}

HWTEST_F(ConnectHandlerTest, GetPairedDevicesNumTest, TestSize.Level1)
{
    uint16_t number = 0;
    
    SleErrorCode ret = g_connectInstance->GetPairedDevicesNum(number);
    
    // 验证返回值是否符合预期
    EXPECT_EQ(ret, SLE_ERR_CHECKOUT_FAILED);
    EXPECT_GE(number, 0);  // 确保设备数量不是负数
}

// 测试 GetPairedDevices
HWTEST_F(ConnectHandlerTest, GetPairedDevicesTest, TestSize.Level1) {
    std::vector<SleDeviceAddress> devices;
    uint16_t number = 0;

    SleErrorCode ret = g_connectInstance->GetPairedDevices(devices, number);
    
    // 这里假设函数应该返回 SLE_ERR_OK 并且至少返回一个设备
    EXPECT_EQ(ret, SLE_ERR_CHECKOUT_FAILED);
}

// 测试 GetPairState
HWTEST_F(ConnectHandlerTest, GetPairStateTest, TestSize.Level1) {
    SleDeviceAddress address = {};
    char addrStr[] = "00:11:aa:bb:f1:6b";
    Str2Mac(address.addr, addrStr);
    SlePairState state;
    SleErrorCode ret = g_connectInstance->GetPairState(address, state);
    EXPECT_EQ(ret, SLE_ERR_CHECKOUT_FAILED);
}

// 测试 ReadRemoteRssi
HWTEST_F(ConnectHandlerTest, ReadRemoteRssiTest, TestSize.Level1) {
    uint16_t connectId = 1;
    int8_t rssi = 0;

    SleErrorCode ret = g_connectInstance->ReadRemoteRssi(connectId, rssi); 
    EXPECT_EQ(ret, SLE_ERR_CHECKOUT_FAILED);
}

}
} // namespace NetManagerStandard
} // namespace OHOS
